#include"sqlite.hpp"
#include<cassert>

//typedef int (*SqliteCallback)(void*, int, char**, char**)
int select_stu_callback(void* arg, int col_count, char** result, char** fileds_name)
{
    std::vector<std::string> *array = (std::vector<std::string>*)arg;
    array->push_back(result[0]);            //查询结果      每一个元素都是一列
    return 0;                               //返回0 必须有！！！
}

int main()
{
    SqliteHelper helper("./test.db");

    //1. 创建/打开文件
    assert(helper.open());
    //2. 创建表(不存在则创建) 学生信息： 学号  姓名 年龄
    const char* ct = "create table if not exists student(sn int primary key, name varchar(32), age int)";
    assert(helper.execute(ct, nullptr, nullptr));   //创建表不需要回调函数
    //3. 新增数据 修改 删除
    const char* insert_sql = "insert into student values(1, '小明', 11), (2, '小红', 18), (3, '小黑', 88)";
    assert(helper.execute(insert_sql, nullptr, nullptr)); 

    const char* update_sql = "update student set name='张小明' where sn=1";
    assert(helper.execute(update_sql, nullptr, nullptr)); 

    const char* delete_sql = "delete from student where sn=3";
    assert(helper.execute(delete_sql, nullptr, nullptr)); 

    const char* select_sql = "select name from student";
    std::vector<std::string> array;
    assert(helper.execute(select_sql, select_stu_callback, &array)); 
    for(auto &it : array)
    {
        std::cout << it << std::endl;
    }
    //4. 关闭数据库
    helper.close();

    return 0;
}